﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bonobo.Irc.Client
{
    [Obsolete]
    internal struct IrcRequest
    {
        private const char Space = ' ';
        private const char Colon = ':';

        private readonly String _commandType;
        private readonly String _prefix;
        private readonly String[] _arguments;

        private readonly String _command;

        public IrcRequest(String command, String prefix, params String[] arguments)
        {
            _commandType = command;
            _prefix = prefix;
            _arguments = arguments;
            _command = String.Empty;
            _command = CreateCommand();
        }


        public String Type
        {
            get { return _commandType; }
        }

        public String Prefix
        {
            get { return _prefix; }
        }

        public IList<String> Arguments
        {
            get { return _arguments; }
        }



        public override string ToString()
        {
            return _command;
        }



        // FIXME: use a StringBuilder
        private string CreateCommand()
        {
            var command = (!String.IsNullOrEmpty(_prefix) ? Colon + _prefix + Space : String.Empty)  + _commandType;
            HandleSpaces();

            foreach (var item in _arguments)
            {
                command += Space + item;
            }

            command += IrcProtocol.NewLine;

            return command;
        }

        // FIXME: replace with a regex, 
        //        don't concatenate strings, use StringBuilder
        private void HandleSpaces()
        {
            for (int i = 0; i < _arguments.Length; i++)
            {
                if (_arguments[i].Contains(Space))
                {
                    _arguments[i] = Colon + _arguments[i];
                }
            }
        }



        // FIXME: replace with a regex
        // FIXME: use a command => factor function dictionary, just like
        //        in the other two(?) cases
        public static bool TryParse(String message, out IrcRequest output)
        {
            var factory = new IrcRequestFactory();

            String prefix = GetMessagePrefix(message);
            String msgWithoutPrefix = GetMessageWithoutPrefix(prefix, message);

            String command = GetMessageCommand(msgWithoutPrefix);
            String msgParameters = GetMessageArguments(msgWithoutPrefix, command);

            String[] arguments = ParseArguments(msgParameters);
            
            if (command.Equals(IrcProtocol.Requests.Ping, StringComparison.Ordinal))
            {
                if (!String.IsNullOrEmpty(arguments[0]))
                {
                    output = factory.Ping(arguments[0]);
                    return true;
                }                
            }

            output = new IrcRequest();
            return false;
        }

        // FIXME: replace with a regex
        private static string GetMessageArguments(String messageWithoutPrefix, String command)
        {
            if (messageWithoutPrefix.Length < command.Length + 1)
            {
                return String.Empty;                
            }

            return messageWithoutPrefix.Remove(0, command.Length + 1);
        }

        // FIXME: replace with a regex
        private static string GetMessageWithoutPrefix(String prefix, String message)
        {
            if (String.IsNullOrEmpty(prefix))
            {
                if (message.StartsWith(": "))
                {
                    message.Substring(2);
                }
                return message;
            }
            else
            {
                return message.Remove(0, prefix.Length + 1);
            }
        }

        // FIXME: replace with a regex
        private static String[] ParseArguments(String arguments)
        {
            var list = new List<String>();
            var arr = arguments.ToCharArray();

            var temp = String.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == ':' && i + 1 < arr.Length)
                {
                    list.Add(arguments.Substring(i + 1));
                    break;
                }
                else
                {
                    if (arr[i] != ' ')
                    {
                        temp += arr[i];
                    }
                    else
                    {
                        list.Add(temp);
                        temp = String.Empty;
                    }

                }
            }

            return list.ToArray();
        }

        // FIXME: replace with a regex
        private static String GetMessageCommand(String msgWithoutPrefix)
        {
            return msgWithoutPrefix.Substring(0, msgWithoutPrefix.IndexOf(' '));
        }

        // FIXME: replace with a regex
        private static String GetMessagePrefix(string message)
        {
            if (message.StartsWith(":"))
            {
                return message.Substring(1, message.IndexOf(' '));
            }

            return String.Empty;
        }


    }
}
